//
//  MHComposeHeaderView.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/11/6.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHComposeHeaderView.h"
#import "MHStatusComposeTextParser.h"
#import "MHSelectPictureCollectionViewCell.h"
#import "MHUploadImage.h"
@implementation MHComposeHeaderView


-(instancetype)initWithFrame:(CGRect)frame type:(MHCirclComposeType ) type
{
    self = [super initWithFrame:frame];
    if (self ) {
        self.type = type;
        [self _setupSubViews];
        
    }
    return self;
    
}

-(void) _setupSubViews
{
    [self addSubview:self.videoView];
    [self addSubview:self.textView];
    if(self.type != MHCirclComposeTypeVideo)
    {
        [self addSubview:self.selectPicView];
    }
    
}


-(MHMomentVideoView *)videoView
{
    if (!_videoView) {
        _videoView = [MHMomentVideoView videoView];
    }
    
    return _videoView;
}

-(MHCommonCollectionViewBoxView *)selectPicView
{
    if (!_selectPicView) {
        
        CHTCollectionViewWaterfallLayout *layout = [[CHTCollectionViewWaterfallLayout alloc] init];
        
        layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
        layout.columnCount = 3;
        layout.minimumColumnSpacing = 8;
        layout.minimumInteritemSpacing = 9;
        NSDictionary *modelCellMap = @{ @"MHUploadImage":@"MHSelectPictureCollectionViewCell",};
        __weak __typeof(self)weakSelf = self;
        __block MHDataSource *dataSource = [[MHDataSource alloc] initWithModelCellMap:modelCellMap cellConfigBlock:^(MHSelectPictureCollectionViewCell *cell, MHUploadImage *item, NSIndexPath *indexPath) {
            cell.imageVIew.image = item.image;
            
//            cell.delBtn.hidden =
            
            cell.delBtn.hidden = item.hiddenDel;
//            if (self.type == MHCirclComposeTypeAsk) {
//                cell.delBtn.hidden = item.hiddenDel;
//            }else
//            {
//                 cell.delBtn.hidden = (indexPath.item == dataSource.items.count -1) && (indexPath.item != 8)&&(self.type != MHCirclComposeTypeAsk);
//            }
            cell.delBtn.tag = indexPath.item;
            [cell.delBtn bk_whenTapped:^{
                if (weakSelf.deletePohotoBlock) {
                    weakSelf.deletePohotoBlock(cell.delBtn.tag);
                }
            }];
            
        }];
        dataSource.collectionViewLayoutSize = ^CGSize(WFDataSourceSection *sectionItem, UICollectionViewLayout *collectionViewLayout, NSIndexPath *indexPath) {
            return CGSizeMake(106 , 103);
            
        };
        dataSource.collectionSectionInset = ^UIEdgeInsets(WFDataSourceSection *sectionItem, NSInteger section) {
            return UIEdgeInsetsMake(5, 5, 5, 5);
        };
        
        dataSource.didSelectCellBlock = ^(MHUploadImage *item, NSIndexPath *indexPath) {
            if ((indexPath.item == dataSource.items.count -1)&& self.type == MHCirclComposeTypePublish) {
                if (self.openCameraBlock) {
                    self.openCameraBlock();
                }
            }else if(self.type == MHCirclComposeTypeAsk)
            {
                if (self.openCameraBlock) {
                    self.openCameraBlock();
                }
            }
        };
        
        _selectPicView = [[MHCommonCollectionViewBoxView alloc] initWithFrame:CGRectZero layout:layout dataSouce:dataSource];
    }
    return _selectPicView;
}

-(XFEmotionTextView *)textView
{
    if (!_textView) {
        _textView = [[XFEmotionTextView alloc]init];
        _textView.font = [UIFont systemFontOfSize:16];
        _textView.placeholderText  = @"说点什么吧...";
        _textView.allowsCopyAttributedString = NO;
        _textView.inputAccessoryView = [UIView new];
        _textView.textParser = [MHStatusComposeTextParser new];
    }
    return _textView;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.videoView.hidden = !(self.type == MHCirclComposeTypeVideo);
    [self.videoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-MHCommonConentInset18);
        make.top.equalTo(self).offset(19);
        make.width.equalTo(@90);
        make.height.equalTo(@127);
    }];
    
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(17);
        make.top.equalTo(self).offset(MHCommonConentInset12);
        
        
        if (self.type == MHCirclComposeTypeVideo) {
            make.right.equalTo(self.videoView.mas_left).offset(-26);
            make.bottom.equalTo(self);
        }else
        {
            make.right.equalTo(self).offset(-17);
            make.bottom.equalTo(self.selectPicView.mas_top);
        }
    }];
    
    
}

@end
